/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.controller;

import com.apache.api.api.ApiService;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SystemTools;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.tools.ConfigUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.PBOSSOTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * description:  服务端接口(返回类型,跳转,分级授权,是否需要验证登录，那些功能点，等)
 *
 * @author iussoft 创建时间：2016-11-3
 */
@Controller
@RequestMapping("uct/admin")
public class UctAdminAction {

    @Autowired
    private ApiService uctService;

    /**
     * description: 根据条件获取用户信息
     */
    @RequestMapping("/user!Info.action")
    @ResponseBody
    public Object userInfo(HttpServletRequest request) {
        ResultEntity entity = getObjInfo(request, "userEntity");
        return entity;
    }

    /**
     * description: 根据条件获取用户信息列表(分页)
     */
    @RequestMapping("/user!list.action")
    @ResponseBody
    public Object listUsers(HttpServletRequest request) {
        ResultEntity entity = getUsers(request, false);
        return entity;
    }

    /**
     * description: 根据条件获取用户信息列表(分页)
     */
    @RequestMapping("/user!data.action")
    @ResponseBody
    public Object dataUsers(HttpServletRequest request, String page, String rows) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        ResultEntity entity = getUsers(request, true);
        if ("true".equals(entity.getResult()) && Validator.isEmpty(entity.getEntity())) {
            if (entity.getEntity() instanceof Page) {
                Page pageList = (Page) entity.getEntity();
                mapData.put("total", pageList.getCount());
                mapData.put("rows", pageList.getPageObjects());
            }
        }
        return mapData;
    }

    /**
     * description: 根据条件获取机构信息
     */
    @RequestMapping("/org!Info.action")
    @ResponseBody
    public Object orgInfo(HttpServletRequest request) {
        ResultEntity entity = getObjInfo(request, "orgEntity");
        return entity;
    }

    /**
     * description: 根据条件获取部门信息
     */
    @RequestMapping("/dept!Info.action")
    @ResponseBody
    public Object deptInfo(HttpServletRequest request) {
        ResultEntity entity = getObjInfo(request, "deptEntity");
        return entity;
    }

    /**
     * description: 根据条件获取权限信息
     */
    @RequestMapping("/act!Info.action")
    @ResponseBody
    public Object actInfo(HttpServletRequest request) {
        ResultEntity entity = getObjInfo(request, "actEntity");
        return entity;
    }

    /**
     * description: 根据条件获取机构信息列表
     */
    @RequestMapping("/org!list.action")
    @ResponseBody
    public Object listOrgs(HttpServletRequest request) {
        ResultEntity entity = getOrgs(request, false);
        return entity;
    }

    /**
     * description: 根据条件获取机构信息列表(分页)
     */
    @RequestMapping("/org!data.action")
    @ResponseBody
    public Object dataOrgs(HttpServletRequest request, String page, String rows) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        ResultEntity entity = getOrgs(request, true);
        if ("true".equals(entity.getResult()) && Validator.isEmpty(entity.getEntity())) {
            if (entity.getEntity() instanceof Page) {
                Page pageList = (Page) entity.getEntity();
                mapData.put("total", pageList.getCount());
                mapData.put("rows", pageList.getPageObjects());
            }
        }
        return mapData;
    }

    /**
     * description: 根据条件获取部门信息列表
     */
    @RequestMapping("/dept!list.action")
    @ResponseBody
    public Object listDepts(HttpServletRequest request) {
        ResultEntity entity = getInfos(request, false, "deptList");
        return entity;
    }

    /**
     * description: 根据条件获取部门信息列表(分页)
     */
    @RequestMapping("/dept!data.action")
    @ResponseBody
    public Object dataDepts(HttpServletRequest request, String page, String rows) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        ResultEntity entity = getInfos(request, true, "deptList");
        if ("true".equals(entity.getResult()) && Validator.isEmpty(entity.getEntity())) {
            if (entity.getEntity() instanceof Page) {
                Page pageList = (Page) entity.getEntity();
                mapData.put("total", pageList.getCount());
                mapData.put("rows", pageList.getPageObjects());
            }
        }
        return mapData;
    }

    /**
     * description: 根据条件获取角色信息列表
     */
    @RequestMapping("/role!list.action")
    @ResponseBody
    public Object listRoles(HttpServletRequest request) {
        ResultEntity entity = getRoles(request, false);
        return entity;
    }

    /**
     * description: 根据条件获取角色信息列表(分页)
     */
    @RequestMapping("/role!data.action")
    @ResponseBody
    public Object dataRoles(HttpServletRequest request, String page, String rows) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        ResultEntity entity = getRoles(request, true);
        if ("true".equals(entity.getResult()) && Validator.isEmpty(entity.getEntity())) {
            if (entity.getEntity() instanceof Page) {
                Page pageList = (Page) entity.getEntity();
                mapData.put("total", pageList.getCount());
                mapData.put("rows", pageList.getPageObjects());
            }
        }
        return mapData;
    }

    /**
     * description: 根据条件获取资源信息列表
     */
    @RequestMapping("/lock!list.action")
    @ResponseBody
    public Object listLocks(HttpServletRequest request) {
        ResultEntity entity = getInfos(request, false, "lockList");
        return entity;
    }

    /**
     * description: 根据条件获取资源信息列表(分页)
     */
    @RequestMapping("/lock!data.action")
    @ResponseBody
    public Object dataLocks(HttpServletRequest request, String page, String rows) {
        Map<String, Object> mapData = new HashMap<String, Object>();
        ResultEntity entity = getInfos(request, true, "lockList");
        if ("true".equals(entity.getResult()) && Validator.isEmpty(entity.getEntity())) {
            if (entity.getEntity() instanceof Page) {
                Page pageList = (Page) entity.getEntity();
                mapData.put("total", pageList.getCount());
                mapData.put("rows", pageList.getPageObjects());
            }
        }
        return mapData;
    }

    /**
     * description:  获取用户列表
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private ResultEntity getUsers(HttpServletRequest request, boolean isPage) {
        Map map = getMethodParams(request);
        ParamsVo vo = new ParamsVo();
        vo.setMethodKey("userList");
        if (Validator.isNull(request.getParameter("orgId"))) {
            LoginUser loginUser = PBOSSOTools.getLoginUser(request);
            //判断是否开启管理子机构
            if ("1".equals(SystemTools.getInstance().getValue("org_act_switch")) && "0"
                    .equals(loginUser.getSysFlag())) {
                map.put("orgId", loginUser.getOrgId());
            }
        }
        vo.setParams(map);
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        if (isPage) {
            vo.setParams("pageIndex", Validator.getDefaultStr(request.getParameter("page"), "1"));
            vo.setParams("pageSize", Validator.getDefaultStr(request.getParameter("rows"), "10"));
        }
        ResultEntity entity = uctService.doService(vo);
        return entity;
    }

    /**
     * description:  获取机构列表
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private ResultEntity getOrgs(HttpServletRequest request, boolean isPage) {
        Map map = request.getParameterMap();
        ParamsVo vo = new ParamsVo();
        vo.setMethodKey("roleList");
        if (Validator.isNull(request.getParameter("orgId"))) {
            LoginUser loginUser = PBOSSOTools.getLoginUser(request);
            //判断是否开启管理子机构
            if ("1".equals(SystemTools.getInstance().getValue("org_act_switch")) && "0"
                    .equals(loginUser.getSysFlag())) {
                map.put("orgId", loginUser.getOrgId());
            }
        }
        vo.setParams(map);
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        if (isPage) {
            vo.setParams("pageIndex", Validator.getDefaultStr(request.getParameter("page"), "1"));
            vo.setParams("pageSize", Validator.getDefaultStr(request.getParameter("rows"), "10"));
        }
        ResultEntity entity = uctService.doService(vo);
        return entity;
    }

    /**
     * description:  获取角色列表
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private ResultEntity getRoles(HttpServletRequest request, boolean isPage) {
        Map map = request.getParameterMap();
        ParamsVo vo = new ParamsVo();
        vo.setMethodKey("orgList");
        if (Validator.isNull(request.getParameter("orgId"))) {
            LoginUser loginUser = PBOSSOTools.getLoginUser(request);
            //判断是否开启管理子机构
            if ("1".equals(SystemTools.getInstance().getValue("org_act_switch")) && "0"
                    .equals(loginUser.getSysFlag())) {
                map.put("orgId", loginUser.getOrgId());
            }
        }
        vo.setParams(map);
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        if (isPage) {
            vo.setParams("pageIndex", Validator.getDefaultStr(request.getParameter("page"), "1"));
            vo.setParams("pageSize", Validator.getDefaultStr(request.getParameter("rows"), "10"));
        }
        ResultEntity entity = uctService.doService(vo);
        return entity;
    }

    /**
     * description:  获取信息列表
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private ResultEntity getInfos(HttpServletRequest request, boolean isPage, String methodKey) {
        Map map = request.getParameterMap();
        ParamsVo vo = new ParamsVo();
        vo.setMethodKey(methodKey);
        vo.setParams(map);
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        if (isPage) {
            vo.setParams("pageIndex", Validator.getDefaultStr(request.getParameter("page"), "1"));
            vo.setParams("pageSize", Validator.getDefaultStr(request.getParameter("rows"), "10"));
        }
        ResultEntity entity = uctService.doService(vo);
        return entity;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private ResultEntity getObjInfo(HttpServletRequest request, String methodKey) {
        Map map = request.getParameterMap();
        ParamsVo vo = new ParamsVo();
        vo.setMethodKey("userEntity");
        vo.setParams(map);
        vo.setParams("sysPass", ConfigUtil.getInstance().interfacePass());
        ResultEntity entity = uctService.doService(vo);
        return entity;
    }

    @SuppressWarnings("rawtypes")
    public Map<String, Object> getMethodParams(HttpServletRequest request) {
        Map<String, Object> bodyMap = new HashMap<String, Object>();
        Set keSet = request.getParameterMap().entrySet();
        for (Iterator itr = keSet.iterator(); itr.hasNext(); ) {
            Map.Entry me = (Map.Entry) itr.next();
            Object ok = me.getKey();
            Object ov = me.getValue();
            String[] value = new String[1];
            if (ov instanceof String[]) {
                value = (String[]) ov;
            } else {
                value[0] = ov.toString();
            }
            if (value.length > 0) {
                bodyMap.put(ok.toString(), value[0]);
                request.setAttribute(ok.toString(), value[0]);
            }
        }
        String sysPass = ConfigUtil.getInstance().interfacePass();
        bodyMap.put("sysPass", sysPass);
        return bodyMap;
    }
}
